package com.zx.oa.controller;

import com.alibaba.fastjson.JSON;
import com.zx.oa.entity.MarketClass;
import com.zx.oa.service.marketTypeClass.MarketTypeClassService;
import com.zx.oa.service.marketTypeClass.MarketTypeClassServiceImpl;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet("/marketStudentAjax.do")
public class MarketStudentAjax extends HttpServlet {

    MarketTypeClassService marketTypeClassService=new MarketTypeClassServiceImpl();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String operation=request.getParameter("operation");
        if("type".equals(operation)){
            typeServlet(request,response);
        } else if ("class".equals(operation)) {
            classServlet(request,response);
        }
    }

    private void classServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        String stuType=request.getParameter("stuType");
        List<MarketClass> marketClassList=marketTypeClassService.getMarketClassInfoByType(stuType);
        String jsonResult= JSON.toJSONString(marketClassList);
        PrintWriter out=response.getWriter();
        out.print(jsonResult);
        out.flush();
        out.close();
    }

    private void typeServlet(HttpServletRequest request, HttpServletResponse response) throws IOException {
        response.setContentType("text/html;charset=utf-8");
        //获取所有市场类型
        List<String> typeList=marketTypeClassService.getClassTypeAll();
        String jsonResult= JSON.toJSONString(typeList);
        PrintWriter out=response.getWriter();
        out.print(jsonResult);
        out.flush();
        out.close();
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
